<template>
  <el-dialog :visible.sync="visible" v-dialogDrag  :close-on-click-modal="false" class="tl-dialog" width="900px" @open="open" @close="close" destroy-on-close>
    <div slot="title" style="font-family: '思源黑体 CN', sans-serif; font-weight: 400; font-style: normal; font-size: 16px !important;">
      {{moduleTitle}}
    </div>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="70px" :disabled="disableEditSubmit" style="padding-top: 11px">
			<el-row v-show="false">
				<el-col :span="12">
					<el-form-item label="评论人id" prop="userid" >
						<el-input-number v-model="model.userid" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-show="false">
				<el-col :span="24">
					<el-form-item label="评论人姓名" prop="username" >
						<el-input v-model="model.username" type="textarea" maxlength="250" show-word-limit :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-show="false">
				<el-col :span="12">
					<el-form-item label="评论人工号" prop="usercode" >
						<el-input v-model="model.usercode"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-show="false">
				<el-col :span="24">
					<el-form-item label="来源类型" prop="sourcetype" >
						<el-input v-model="model.sourcetype" type="textarea" :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-show="false">
				<el-col :span="12">
					<el-form-item label="来源id" prop="sourceid" >
						<el-input-number v-model="model.sourceid" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="24">
					<el-form-item label="留言对象" prop="content" >
						<el-input v-model="model.target" disabled></el-input>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="留言内容" prop="content">
            <el-input v-model="model.content" type="textarea" :rows="4" maxlength="1000" show-word-limit></el-input>
          </el-form-item>
        </el-col>
      </el-row>
			<el-row v-show="false">
				<el-col :span="24">
					<el-form-item label="备注" prop="description" >
						<el-input v-model="model.description" type="textarea" maxlength="1000" show-word-limit :rows="4"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row v-show="false">
				<el-col :span="12">
					<el-form-item label="排序码" prop="sortcode">
						<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="有效" prop="enabled">
						<el-radio-group v-model="model.enabled">
							<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <history-list ref="historyList" :source-type="sourceType" :query-self-only="true" :query-read-record="true"></history-list>
    <div slot="footer" class="dialog-footer">
      <el-button style="padding: 7px 15px;border: 1px solid;"  :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
import historyList from "./historyList"
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { historyList },
  data() {
    return {
      moduleTitle: '新增留言',
      urlPrefix: '/mpm/comments/',
      url: {
        // 额外的url
        list: '/mpm/comments/queryCommentList'
      },
      parentModel: {}
    }
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    }
  },
  methods: {
    open() {
      this.model = {}
      this.$nextTick(() => {
        this.$refs.historyList.sourceId = this.parentModel.id
        this.$refs.historyList.queryCommentList()
        this.$refs.form.clearValidate()
        if (this.isAdd === true) {
          this.title = '添加' + this.moduleTitle
          // 需要添加重复判断
          if (this.$store.getters.param.isAvoidRepeat === true) {
            this.queryBillUid()
          }
          if (this.isQueryInitData === true) {
            // 后台进行数据初始化
            this.queryInitData().then((_) => {
              this.assignDefaultValue()
              this.$refs.form.clearValidate()
              this.loaded()
            })
          } else {
            // 前台进行数据初始化
            this.$set(this.model, 'enabled', 1)
            this.$set(this.model, 'sortcode', 0)
            this.assignDefaultValue()
            this.$refs.form.clearValidate()
            this.loaded()
          }
        } else if (this.disableEditSubmit === false) {
          this.title = '编辑' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.loaded()
          })
        } else {
          this.title = '查看' + this.moduleTitle
          this.queryData(this.id).then((_) => {
            this.$refs.form.clearValidate()
            this.loaded()
          })
        }
      })
    },
    loaded() {
      // 页面加载完毕执行
    },
    assignDefaultValue() {
      this.$set(this.model, "target", this.parentModel.target)
      this.$set(this.model, "sourcetype", this.sourceType)
      this.$set(this.model, "sourceid", this.parentModel.id)
    },
    /** 提交前执行 */
    beforeSubmit() {
      if (this.model.content) {
        return true
      }
      this.$tap.message("请填写留言内容！", 'warning')
      return false
    },
    /** 执行新增提交 */
    doAdd() {
      this.saveLoading = true
      // 增加唯一单号
      postAction(this.url.add + '?billuid=' + this.uid, this.model)
        .then((res) => {
          if (res.success) {
            this.$tap.message('添加成功', 'success')
            this.model = res.result
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterSubmit()
          this.saveLoading = false
          this.initModel()
          this.$refs.historyList.queryCommentList()
        })
        .catch((err) => {
          this.saveLoading = false
        })
    },
    initModel() {
      this.$refs.form.clearValidate()
      this.model = {}
      this.assignDefaultValue()
    }
  }
}
</script>
<style lang="less" scoped>
   /deep/.el-dialog__body {
    padding: 5px 20px !important;
    padding-bottom: 0;
  }
   /deep/ .el-dialog__footer {
     padding: 10px 20px 20px !important;
   }
  /deep/ .el-form-item--mini.el-form-item {
     margin-right: 0;
  }
</style>

